//
//  LGReportDetailLRCell.m
//  HeartDiseaseManage
//
//  Created by Bovin on 2023/5/25.
//  检查结果cell

#import "LGReportDetailLRCell.h"

#define nameWidth     (isPad ? viewPix(280) : viewPix(110))

#define resultWidth   (isPad ? viewPix(140) : viewPix(70))

#define itemWidth   (Screen_W-viewPix(32)-viewPix(16)-viewPix(15)-nameWidth-resultWidth)/2.0

@interface LGReportDetailLRCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *resultLabel;
/**   */
@property (nonatomic , strong)UILabel *arrowLabel;
/**   */
@property (nonatomic , strong)UILabel *rangeLabel;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;

@end

@implementation LGReportDetailLRCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.nameLabel.text = LGNSString(dataDic[@"name"]);
    self.resultLabel.text = LGNSString(dataDic[@"result"]);
    self.arrowLabel.text = LGNSString(dataDic[@"arrow"]);
    self.rangeLabel.text = LGNSString(dataDic[@"range"]);
    self.unitLabel.text = LGNSString(dataDic[@"unit"]);

    NSString *status = LGNSString(dataDic[@"arrow_status"]);
    NSString *color = @"#333333";
    if([status isEqualToString:@"up"]){
        color = @"#DD3030";
    }else if ([status isEqualToString:@"down"]){
        color = @"#4C87FD";
    }else{
        color = @"#333333";
    }
    self.resultLabel.textColor = [UIColor colorWithString:color];
    self.arrowLabel.textColor = [UIColor colorWithString:color];
    CGFloat width = [LGNSString(dataDic[@"result"]) textWidthWithFont:LGFontWeight(13, UIFontWeightMedium) maxHeight:MAXFLOAT];
    [self.arrowLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.resultLabel.mas_left).offset(width+3);
    }];
    
    //计算最大高度
    NSString *result = [NSString stringWithFormat:@"%@%@",dataDic[@"result"],dataDic[@"arrow"]];
    CGFloat nameHeight = [self.nameLabel.text textHeightWithFont:LGFont(13) maxWidth:nameWidth];
    CGFloat resultHeight = [result textHeightWithFont:LGFontWeight(13, UIFontWeightMedium) maxWidth:resultWidth];
    CGFloat unitHeight = [self.unitLabel.text textHeightWithFont:LGFont(13) maxWidth:itemWidth];
    CGFloat rangeHeight = [self.rangeLabel.text textHeightWithFont:LGFontWeight(13, UIFontWeightMedium) maxWidth:itemWidth];
    CGFloat tempHeight = nameHeight;
    tempHeight = (resultHeight>tempHeight)?resultHeight:tempHeight;
    tempHeight = (unitHeight>tempHeight)?unitHeight:tempHeight;
    tempHeight = (rangeHeight>tempHeight)?rangeHeight:tempHeight;
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(viewPix(44)+tempHeight+3));
    }];
}


-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.resultLabel];
    [self.contentView addSubview:self.arrowLabel];
    [self.contentView addSubview:self.unitLabel];
    [self.contentView addSubview:self.rangeLabel];
    
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(44)+16));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.top.equalTo(self.contentView).offset(viewPix(6));
        make.width.equalTo(@(nameWidth));
    }];
    
    [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(5));
        make.top.equalTo(self.nameLabel);
        make.width.equalTo(@(resultWidth));
    }];
    
    [self.arrowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.resultLabel.mas_left).offset(0);
        make.centerY.equalTo(self.resultLabel);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.resultLabel.mas_right).offset(viewPix(5));
        make.top.equalTo(self.resultLabel);
        make.width.equalTo(@(itemWidth));
    }];
    [self.rangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.unitLabel.mas_right).offset(viewPix(5));
        make.top.equalTo(self.resultLabel);
        make.width.equalTo(@(itemWidth));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _nameLabel;
}

-(UILabel *)arrowLabel{
    if(!_arrowLabel){
        _arrowLabel = [UILabel lableWithText:@"" colorString:@"#EB5757" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _arrowLabel;
}

-(UILabel *)resultLabel{
    if(!_resultLabel){
        _resultLabel = [UILabel lableWithText:@"结果" colorString:@"#333333" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _resultLabel;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"单位" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _unitLabel;
}

-(UILabel *)rangeLabel{
    if(!_rangeLabel){
        _rangeLabel = [UILabel lableWithText:@"参考值" colorString:@"#333333" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _rangeLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

